@extends('backend.layouts.default')

@section('title')添加管理员@endsection

@section('brief')您可以在此页面添加管理员@endsection

@section('content')
@include('backend.commons.sidebar')
        <!-- BEGIN CONTENT -->
<div class="page-content-wrapper">
    <!-- BEGIN CONTENT BODY -->
    <div class="page-content">
        <!-- BEGIN PAGE HEADER-->
        @include('backend.commons.title')
        <div class="page-bar">
            <ul class="page-breadcrumb">
                <li>
                    <i class="icon-home"></i>
                    <a href="{{ url('/admin/dashboard') }}">管理首页</a>
                    <i class="fa fa-angle-right"></i>
                </li>
                <li>
                    <a href="{{ url('/admin/admins') }}">管理员管理</a>
                    <i class="fa fa-angle-right"></i>
                </li>
                <li>
                    <span>@yield('title')</span>
                </li>
            </ul>
        </div>
        <!-- END PAGE HEADER-->

        <!-- BEGIN ADD -->
        <div class="portlet light ">
            <div class="portlet-body form">
                <!-- BEGIN FORM-->
                <form  id="createAdmin" class="form-horizontal">
                    <div class="form-body">
                        <div class="form-group">
                            <label class="col-md-3 control-label"><span class="required" aria-required="true"> * </span>名称</label>
                            <div class="col-md-4">
                                <input type="text" name="name" class="form-control" placeholder="显示名称">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-3 control-label"><span class="required" aria-required="true"> * </span>账号</label>
                            <div class="col-md-4">
                                <input type="text" name="email" class="form-control" placeholder="邮箱">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-3 control-label"><span class="required" aria-required="true"> * </span>角色</label>
                            <div class="col-md-2">
                                <select name="roleId" class="form-control input-sm select2">
                                    <option value=""></option>
                                    @foreach($roles as $role)
                                        <option value="{{ $role->id }}">{{ $role->display_name }}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-3 control-label"><span class="required" aria-required="true"> * </span>密码</label>
                            <div class="col-md-4">
                                <input type="password" name="password" class="form-control" placeholder="8-16 位, 数字英文字母组合">
                            </div>
                        </div>

                    </div>
                    <div class="form-actions">
                        <div class="row">
                            <div class="col-md-offset-3 col-md-4">
                                <a href="javascript:;" id="create" class="btn green">保存</a>
                                <a href="javascript:;" id="createAndContinue" class="btn green">保存并继续添加</a>
                                <a href="{{ url('/admin/admins') }}" type="button" class="btn default">返回列表</a>
                            </div>
                        </div>
                    </div>
                </form>
                <!-- END FORM-->
            </div>
        </div>
        <!-- END EDD -->
    </div>
    <!-- END CONTENT BODY -->

    @push('styles')
    <link href="{{ config('app.static_url') }}assets/global/plugins/select2/css/select2.min.css" rel="stylesheet" type="text/css" />
    <link href="{{ config('app.static_url') }}assets/global/plugins/select2/css/select2-bootstrap.min.css" rel="stylesheet" type="text/css" />
    @endpush

    @push('scripts')
    <script src="{{ config('app.static_url') }}assets/global/plugins/select2/js/select2.full.min.js" type="text/javascript"></script>
    <script>
        /********************
         |
         | 表单处理
         |
         \********************/
        var doCreateFunc = function() {
            var formDom = $('#createAdmin');
            var name = formDom.find('[name=name]').val();
            var email = formDom.find('[name=email]').val();
            var roleId = formDom.find('[name=roleId]').val();
            var password = formDom.find('[name=password]').val();
            var redirect = '{{ url('/admin/admins') }}';

            $.ajax({
                url: '{{ url('/admin/admins') }}',
                type: 'post',
                datatype: 'json',
                formDom: formDom,
                bindDom: $('#create'),
                bindType: 'click',
                bindFunc: doCreateFunc,
                data: {
                    name: name,
                    email: email,
                    roleId: roleId,
                    password:password,
                    redirect:redirect
                }
            });
        };

        var doCreateAndContinueFunc = function() {
            var formDom = $('#createAdmin');
            var name = formDom.find('[name=name]').val();
            var email = formDom.find('[name=email]').val();
            var roleId = formDom.find('[name=roleId]').val();
            var password = formDom.find('[name=password]').val();

            var redirect = '{{ url('/admin/admins/create') }}';

            $.ajax({
                url: '{{ url('/admin/admins') }}',
                type: 'post',
                datatype: 'json',
                formDom: formDom,
                bindDom: $('#createAndContinue'),
                bindType: 'click',
                bindFunc: doCreateAndContinueFunc,
                data: {
                    name: name,
                    email: email,
                    roleId:roleId,
                    password:password,
                    redirect:redirect
                }
            });
        };
        $(function() {
            $(".select2").select2({placeholder:'请选择角色',width: '100%'});

            $('#create').bind('click', doCreateFunc);
            $('#createAndContinue').bind('click', doCreateAndContinueFunc);
        });
    </script>
    @endpush
</div>
@endsection